package com.zzsy.carosshopprojects.activitys.home.mainpage.barmanager.membershipcard;

import android.app.Activity;

import com.king.base.util.ToastUtils;
import com.zzsy.carosshopprojects.base.BaseViewMode;
import com.zzsy.carosshopprojects.base.ConfigureFile;
import com.zzsy.carosshopprojects.bean.BaseBean;
import com.zzsy.carosshopprojects.bean.CardZBBean;
import com.zzsy.carosshopprojects.network.RequestUtils;
import com.zzsy.carosshopprojects.network.Utils.MyObserver;
import com.zzsy.carosshopprojects.util.DataUtil;
import com.zzsy.carosshopprojects.util.StringUtils;
import com.zzsy.carosshopprojects.util.md5.Md5Util;
import com.zzsy.carosshopprojects.view.dialog.ComTwoBtnDialog;

import java.util.HashMap;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;

public class MemberShipCardMode extends BaseViewMode {
    private MutableLiveData<CardZBBean> liveData;
    private MutableLiveData<Integer> liveDataResult;
    private ComTwoBtnDialog buKaDialog;
    private String phone = "";

    public MutableLiveData<CardZBBean> getLiveData() {
        if (liveData == null)
            liveData = new MutableLiveData<>();
        return liveData;
    }

    public MutableLiveData<Integer> getliveDataResult() {
        if (liveDataResult == null)
            liveDataResult = new MutableLiveData<>();
        return liveDataResult;
    }

    public void getCardNo(Activity context, String uuid) {
        RequestUtils.CarPreparation(context, DataUtil.getInstance().getStoreId(), uuid, new MyObserver<CardZBBean>(context) {
            @Override
            public void onSuccess(CardZBBean result) {
                liveData.postValue(result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                ToastUtils.showToast(context, errorMsg);
                context.finish();
            }
        });
    }

    public void checkMember(Activity context, String userName, String userPhone, String psdOne, String psdTwo, String paySwitch, String productId, String carNumber, String uuid, String cardKindId) {
        if (StringUtils.isEmpty(userName)) {
            ToastUtils.showToast(context, "请输入会员名称！");
            return;
        }
        if (StringUtils.isEmpty(userPhone)) {
            ToastUtils.showToast(context, "请输入电话号！");
            return;
        }
        if (userPhone.length() != 11) {
            ToastUtils.showToast(context, "请输入正确格式电话号！");
            return;
        }
        if (StringUtils.isEmpty(paySwitch)) {
            ToastUtils.showToast(context, "密码开关异常，重新点击！");
            return;
        } else if ("1".equals(paySwitch)) {
            if (StringUtils.isEmpty(psdOne)) {
                ToastUtils.showToast(context, "请输入密码！");
                return;
            }
            if (StringUtils.isEmpty(psdTwo)) {
                ToastUtils.showToast(context, "请输入确认密码！");
                return;
            }
            if (psdOne.length() != 6) {
                ToastUtils.showToast(context, "请输入6位格式的密码！");
                return;
            }
            if (psdTwo.length() != 6) {
                ToastUtils.showToast(context, "请输入6位格式的确认密码！");
                return;
            }
            if (!psdOne.equals(psdTwo)) {
                ToastUtils.showToast(context, "确认密码与密码不同！");
                return;
            }
        }

        if (StringUtils.isEmpty(productId)) {
            ToastUtils.showToast(context, "请设置常用油品！");
            return;
        }

        if (StringUtils.isEmpty(carNumber)) {
            ToastUtils.showToast(context, "获取卡号异常，请重新刷卡！");
            return;
        }
        phone = userPhone;
        Map<String, String> map = new HashMap<>();
        map.put("cardNo", carNumber);
        map.put("companyId", DataUtil.getInstance().getCompanyId());
        map.put("name", userName);
        map.put("productId", productId);
        map.put("password", psdOne);
        map.put("paySwitch", paySwitch);
        map.put("phone", userPhone);
        map.put("source", "3");
        map.put("staffId", "");
        map.put("storeId", DataUtil.getInstance().getStoreId());
        map.put("uuid", uuid);
        map.put("cardKindId", cardKindId);
        getToken(context, token -> {
            memberCardData(context, token, map);
        });

    }

    private void memberCardData(Activity context, String tokens, Map<String, String> map) {
        /**
         * 通过密码开关，配置密码
         * */
        String psdStr = "";
        if ("1".equals(map.get("paySwitch"))) {
            psdStr = Md5Util.md5Hex(map.get("password"));
        } else if ("0".equals(map.get("paySwitch"))) {
            psdStr = "";
        }
        map.put("password", psdStr);
        RequestUtils.CreateMainCard(context, map, tokens, new MyObserver<BaseBean>(context) {
            @Override
            public void onSuccess(BaseBean result) {
                liveDataResult.postValue(1);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg, int code) {
                if (code == 101) {
                    showDg(context);
                    return;
                } else if (code != 200) {
                    ToastUtils.showToast(context, errorMsg);
                    return;
                } else if (code == ConfigureFile.code) {
                    liveDataResult.postValue(1);
                }

            }
        });

    }

    private void showDg(Activity context) {
        if (buKaDialog != null) {
            buKaDialog.dismiss();
        }
        buKaDialog = new ComTwoBtnDialog(context, ComTwoBtnDialog.BUKA_CARD, null);
        buKaDialog.show();
        buKaDialog.setDialogBtnClickListener(new ComTwoBtnDialog.IDialogBtnClickListener() {
            @Override
            public void onDialogLeftBtnClick() {

            }

            @Override
            public void onDialogRightBtnClick() {
                reqLogoutData();
            }
        });
    }

    private void reqLogoutData() {
        liveDataResult.postValue(2);
    }
}
